import json

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
from django.views import View


class ParamsView(View):

    def get(self, request,mobile):
        # 解析 查询参数 GET
        # params = request.GET
        # name = params.get('name')
        # age = params.getlist('age')
        # return HttpResponse(f'测试 {params}\n{type(params)}\nname={name}\nage={age}')

        # 路由解析
        # return HttpResponse(f'测试 {mobile}\n{type(mobile)}')

        # 解析请求头
        headers = request.META
        print('请求方式', request.method)
        print('文件', request.FILES)
        print('浏览器缓存', request.COOKIES)
        print('用户', request.user)
        return  HttpResponse('测试')

    def post(self, request):
        # 解析 请求体 form参数 POST
        # data = request.POST
        # # print(data)
        # # print(type(data))
        # # print(data.get('name'))
        # # print(data.getlist('sex'))
        # name = data.get('name')
        # sex = data.getlist('sex')
        # return HttpResponse(f'测试 {data}\n{type(data)}\nname={name}\nsex={sex}')
        # # return HttpResponse('测试 ')

        # 解析 请求体 json参数 body
        json_byte = request.body
        json_str = json_byte.decode('utf-8')
        json_dict = json.loads(json_str)
        return HttpResponse(f'测试 {json_dict["a"]}\n{json_dict["b"]}\n{json_dict["c"]}')